package com.wf.course.web.controller.ad;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.utils.page.PageBean;
import com.wf.course.model.ad.IndexType;
import com.wf.course.service.IIndexTypeService;
import com.wf.course.web.Interceptor.LogInfo;
/**
 * 
 * @content 课程分类Controller
 */
@RequestMapping("indexType")
@Controller
public class IndexTypeController {

	@Autowired
	private IIndexTypeService indexTypeService;

	/**
	 * 列表展示
	 * @param pageNo
	 * @param pageSize
	 * @param indexTypeName
	 * @param showPosition
	 * @param model
	 * @return
	 */
	@LogInfo("查询首页分类信息")
	@RequestMapping("indexTypeList")
	public String adList(@RequestParam(value = "pageNo", required = false) Integer pageNo,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "indexTypeName", required = false) String indexTypeName,
			@RequestParam(value = "showPosition", required = false) String showPosition,
			Model model) {

		// 组装一个查询条件的map
		Map<String, Object> cons = new HashMap<String, Object>();
		cons.put("indexTypeName", indexTypeName);
		cons.put("showPosition", showPosition);
		PageBean<IndexType> pageBean = new PageBean<IndexType>(pageNo, pageSize, null, cons);
		pageBean.setUrl("indexType/indexTypeList");
		this.indexTypeService.indexInfo(pageBean);
		
		model.addAttribute("page", pageBean);
		return "ad/indexTypeList";
	}

	/**
	 *  修改和新增页面数据初始化
	 * @param id
	 * @param model
	 * @return
	 */
	@LogInfo("新增或修改首页分类，页面数据初始化")
	@RequestMapping("pageLoad/{indexTypeCode}")
	public String downLoadById(@PathVariable("indexTypeCode") Integer indexTypeCode, Model model) {
		 
		if (indexTypeCode != null && indexTypeCode != 0) {
			IndexType indexType = indexTypeService.queryIndexTypeById(indexTypeCode);
			model.addAttribute("indexType", indexType);
			model.addAttribute("type", "upd");
		}else{
			model.addAttribute("type", "add");
		}
		 
		
		return "ad/indexTypeEdit";
	}

	/**
	 * 新增和修改
	 * 
	 * @param indexType
	 * @param imgFile
	 * @param request
	 * @return
	 */
	@LogInfo("新增或修改首页分类")
	@RequestMapping(value = "indexTypeEdit" , produces = "text/html;charset=UTF-8")
	public String indexTypeEdit(@ModelAttribute IndexType indexType, MultipartFile imgFile, HttpServletRequest request) {
		
		indexTypeService.saveOrUpdateIndexType(indexType,imgFile);
		
		return "redirect:/indexType/indexTypeList";
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@LogInfo("删除首页分类")
	@RequestMapping(value = "remove/{indexTypeCode}")
	public String removeAd(@PathVariable("indexTypeCode") Integer indexTypeCode) {
		
		indexTypeService.deleteById(indexTypeCode);
		
		return "redirect:/indexType/indexTypeList";
	}
	

}
